﻿#include "UndoRedoKeyEventFilter.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalEventFilter.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKKeyMouseStates.h"
#include "FITK_Kernel/FITKCore/FITKGUIRepo.h"
#include "FITK_Kernel/FITKCore/FITKAbstractGUIDialog.h"
#include<QDebug>
#include <QKeyEvent>

void UndoRedoKeyEventFilter::eventFilter(QObject *Object, QEvent *event)
{
    QKeyEvent * keyevent = dynamic_cast<QKeyEvent*> (event);
    if (keyevent == nullptr) return;

    //鼠标键盘状态ctrl是否按下
     AppFrame::FITKKeyMouseStates* states = FITKAPP->getGlobalData()->getKeyMouseStates();
     if (states == nullptr || !states->keyPressed(Qt::Key_Control)) return;


     if (keyevent->key() == Qt::Key_Z)
         this->undo(Object);
     else if (keyevent->key() == Qt::Key_Y)
         this->redo(Object);

}

void UndoRedoKeyEventFilter::undo(QObject* obj)
{

    qDebug() << obj;
    Core::FITKDialogRevokeable* d = Core::FITKGUIRepo::getInstance()->getActiveDialog<Core::FITKDialogRevokeable>();
    if (d && d == obj)
        d->undo();
}

void UndoRedoKeyEventFilter::redo(QObject* obj)
{
    Core::FITKDialogRevokeable* d = Core::FITKGUIRepo::getInstance()->getActiveDialog<Core::FITKDialogRevokeable>();
    if (d&& d == obj) 
        d->redo();
}

